<?php

/**
 * add field to user profiles
 */
 
class WPSPO_User_Avatars {
	private $user_id_being_edited;
	
	public function __construct() {
		add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );
		
		add_action( 'admin_init', array( $this, 'admin_init' ) );
		
		add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
		add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
		
		add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );
		add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
		
		add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );
	}
	
	public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = false ) {
		
		if ( is_numeric($id_or_email) )
			$user_id = (int) $id_or_email;
		elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
			$user_id = $user->ID;
		elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )
			$user_id = (int) $id_or_email->user_id;
		
		if ( empty( $user_id ) )
			return $avatar;
		
		$local_avatars = get_user_meta( $user_id, 'wpspo_user_avatar', true );
		
		if ( empty( $local_avatars ) || empty( $local_avatars['full'] ) )
			return $avatar;
		
		$size = (int) $size;
			
		if ( empty( $alt ) )
			$alt = get_the_author_meta( 'display_name', $user_id );
			
		// generate a new size
		if ( empty( $local_avatars[$size] ) ) {
			$upload_path = wp_upload_dir();
			$avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
			$image_sized = image_resize( $avatar_full_path, $size, $size, true );		
			// deal with original being >= to original image (or lack of sizing ability)
			$local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );	
			// save updated avatar sizes
			update_user_meta( $user_id, 'wpspo_user_avatar', $local_avatars );
		} elseif ( substr( $local_avatars[$size], 0, 4 ) != 'http' ) {
			$local_avatars[$size] = home_url( $local_avatars[$size] );
		}
		
		$author_class = is_author( $user_id ) ? ' current-author' : '' ;
		$avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
		
		return apply_filters( 'wpspo_user_avatar', $avatar );
	}
	
	public function admin_init() {
		//load_plugin_textdomain( 'wpspo-user-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
        //load_plugin_textdomain('wpspo-user-avatars', PLUGINDIR . '/'.WPSPO_NAME_DIR.'/wpspo-admin/localization');
		
		register_setting( 'discussion', 'wpspo_user_avatars_caps', array( $this, 'sanitize_options' ) );
		add_settings_field( 'wpspo-user-avatars-caps', __('Local Avatar Permissions', 'sponsorpress'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );
	}
	
	public function sanitize_options( $input ) {
		$new_input['wpspo_user_avatars_caps'] = empty( $input['wpspo_user_avatars_caps'] ) ? 0 : 1;
		return $new_input;
	}
	
	public function avatar_settings_field( $args ) {		
		$options = get_option('wpspo_user_avatars_caps');
		
		echo '
			<label for="wpspo_user_avatars_caps">
				<input type="checkbox" name="wpspo_user_avatars_caps" id="wpspo_user_avatars_caps" value="1" ' . @checked( $options['wpspo_user_avatars_caps'], 1, false ) . ' />
				' . __('Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'sponsorpress') . '
			</label>
		';
	}
	
	public function edit_user_profile( $profileuser ) {
	?>
	<h3><?php _e( 'Avatar','wpspo-user-avatars' ); ?></h3>
	
	<table class="form-table">
		<tr>
			<th><label for="wpspo-user-avatar"><?php _e('Upload Avatar','wpspo-user-avatars'); ?></label></th>
			<td style="width: 50px;" valign="top">
				<?php echo get_avatar( $profileuser->ID ); ?>
			</td>
			<td>
			<?php
				$options = get_option('wpspo_user_avatars_caps');
			
				if ( empty($options['wpspo_user_avatars_caps']) || current_user_can('upload_files') ) {
					do_action( 'wpspo_user_avatar_notices' ); 
					wp_nonce_field( 'wpspo_user_avatar_nonce', '_wpspo_user_avatar_nonce', false ); 
			?>
					<input type="file" name="wpspo-user-avatar" id="wpspo-user-avatar" /><br />
			<?php
					if ( empty( $profileuser->wpspo_user_avatar ) )
						echo '<span class="description">' . __('No local avatar is set. Use the upload field to add a local avatar.', 'sponsorpress') . '</span>';
					else 
						echo '
							<input type="checkbox" name="wpspo-user-avatar-erase" value="1" /> ' . __('Delete local avatar','wpspo-user-avatars') . '<br />
							<span class="description">' . __('Replace the local avatar by uploading a new avatar, or erase the local avatar (falling back to a gravatar) by checking the delete option.', 'sponsorpress') . '</span>
						';		
				} else {
					if ( empty( $profileuser->wpspo_user_avatar ) )
						echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.', 'sponsorpress') . '</span>';
					else 
						echo '<span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.', 'sponsorpress') . '</span>';
				}
			?>
			</td>
		</tr>
	</table>
	<script type="text/javascript">var form = document.getElementById('your-profile');form.encoding = 'multipart/form-data';form.setAttribute('enctype', 'multipart/form-data');</script>
	<?php		
	}
	
	public function edit_user_profile_update( $user_id ) {
		if ( ! isset( $_POST['_wpspo_user_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_wpspo_user_avatar_nonce'], 'wpspo_user_avatar_nonce' ) )			//security
			return;
	
		if ( ! empty( $_FILES['wpspo-user-avatar']['name'] ) ) {
			$mimes = array(
				'jpg|jpeg|jpe' => 'image/jpeg',
				'gif' => 'image/gif',
				'png' => 'image/png',
				'bmp' => 'image/bmp',
				'tif|tiff' => 'image/tiff'
			);
		
			// front end (theme my profile etc) support
			if ( ! function_exists( 'wp_handle_upload' ) )
				require_once( ABSPATH . 'wp-admin/includes/file.php' );
			
			$this->avatar_delete( $user_id );	// delete old images if successful
			
			// need to be more secure since low privelege users can upload
			if ( strstr( $_FILES['wpspo-user-avatar']['name'], '.php' ) )
				wp_die('For security reasons, the extension ".php" cannot be in your file name.');
			
			$this->user_id_being_edited = $user_id; // make user_id known to unique_filename_callback function
			$avatar = wp_handle_upload( $_FILES['wpspo-user-avatar'], array( 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array( $this, 'unique_filename_callback' ) ) );
			
			if ( empty($avatar['file']) ) {		// handle failures
				switch ( $avatar['error'] ) {
					case 'File type does not meet security guidelines. Try another.' :
						add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("Please upload a valid image file for the avatar.","wpspo-user-avatars"));') );				
						break;
					default :
						add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error","<strong>".__("There was an error uploading the avatar:","wpspo-user-avatars")."</strong> ' . esc_attr( $avatar['error'] ) . '");') );
				}
				
				return;
			}
			
			update_user_meta( $user_id, 'wpspo_user_avatar', array( 'full' => $avatar['url'] ) );		// save user information (overwriting old)
		} elseif ( ! empty( $_POST['wpspo-user-avatar-erase'] ) ) {
			$this->avatar_delete( $user_id );
		}
	}
	
	/**
	 * remove the custom get_avatar hook for the default avatar list output on options-discussion.php
	 */
	public function avatar_defaults( $avatar_defaults ) {
		remove_action( 'get_avatar', array( $this, 'get_avatar' ) );
		return $avatar_defaults;
	}
	
	/**
	 * delete avatars based on user_id
	 */
	public function avatar_delete( $user_id ) {
		$old_avatars = get_user_meta( $user_id, 'wpspo_user_avatar', true );
		$upload_path = wp_upload_dir();
			
		if ( is_array($old_avatars) ) {
			foreach ($old_avatars as $old_avatar ) {
				$old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
				@unlink( $old_avatar_path );	
			}
		}
		
		delete_user_meta( $user_id, 'wpspo_user_avatar' );
	}
	
	public function unique_filename_callback( $dir, $name, $ext ) {
		$user = get_user_by( 'id', (int) $this->user_id_being_edited ); 
		$name = $base_name = sanitize_file_name( $user->display_name . '_avatar' );
		$number = 1;
		
		while ( file_exists( $dir . "/$name$ext" ) ) {
			$name = $base_name . '_' . $number;
			$number++;
		}
				
		return $name . $ext;
	}
}

$wpspo_user_avatars = new WPSPO_User_Avatars;

/**
 * more efficient to call simple local avatar directly in theme and avoid gravatar setup
 * 
 * @param int|string|object $id_or_email A user ID,  email address, or comment object
 * @param int $size Size of the avatar image
 * @param string $default URL to a default image to use if no avatar is available
 * @param string $alt Alternate text to use in image tag. Defaults to blank
 * @return string <img> tag for the user's avatar
 */
function get_wpspo_user_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
	global $wpspo_user_avatars;
	$avatar = $wpspo_user_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
	
	if ( empty ( $avatar ) )
		$avatar = get_avatar( $id_or_email, $size, $default, $alt );
	
	return $avatar;
}

/**
 * on uninstallation, remove the custom field from the users and delete the local avatars
 */

register_uninstall_hook( __FILE__, 'wpspo_user_avatars_uninstall' );

function wpspo_user_avatars_uninstall() {
	$wpspo_user_avatars = new WPSPO_User_Avatars;
	$users = get_users_of_blog();
	
	foreach ( $users as $user )
		$wpspo_user_avatars->avatar_delete( $user->user_id );
	
	delete_option('wpspo_user_avatars_caps');
}
